package com.koalac.ironman.file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.content.res.AssetManager;

public class FileAssetsHandle extends FileBaseHandle{
	final AssetManager assets;

	public FileAssetsHandle(AssetManager assets, String fileName, FileType type) {
		super(fileName, type);
		this.assets = assets;
	}

	public FileAssetsHandle(AssetManager assets, File file, FileType type) {
		super(file, type);
		this.assets = assets;
	}
	
	public InputStream read() {
		if (type == FileType.AssetsType) {
			try {
				return assets.open(file.getPath());
			} catch (IOException ex) {
				System.out.println("Error reading file: " + file + " (" + type
						+ ")" + ex);
			}
		}
		return super.read();
	}
	
	public String getValueByFieldFromAssets(String field) {
		String value = null;
		Properties pos = new Properties();
		try {
			pos.load(this.read());
			value = pos.getProperty("s0");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return null;
		}finally{
			pos.clear();
			pos = null;
		}
		return value;
	}
}
